<?php

class image_uploader
{
	public $responseInfo;
	public $type = 'xml';
	public $user_agent = 'rabr';

	public function imgly($image)
	{
		$postdata = array( 'media' => "@$image");
		$request = 'http://img.ly/api/upload';
		return $this->objectify( $this->process( $request, $postdata ) );
	}

	/**** request method ****/
	function process($url, $postargs=false, $header=false)
	{
		$ch = curl_init($url);
		if ($header === false)
		{
			$header = array("Expect:");
		}
		else
		{
			$header[] = "Expect:";
		}
		
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

		if($postargs !== false)
		{
			curl_setopt ($ch, CURLOPT_POST, true);
			curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs);
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);

		$response = curl_exec($ch);

		$this->responseInfo=curl_getinfo($ch);
		curl_close($ch);

		if( intval( $this->responseInfo['http_code'] ) == 200 )
			return $response;    
		else
			return false;
	}

	function objectify( $data )
	{
		if( function_exists('simplexml_load_string') ) {
			$obj = simplexml_load_string( $data );
		}
		if (isset($obj->error) || !$obj) return false;
		else 
		{
			return $obj;
		}
	}
	
	function imgly2($img)
	{
		if(!isset($_SESSION)){
			session_start();
		}
	
		$postdata = array( 'media' => "@$img");
		$url = 'http://img.ly/api/2/upload.xml';
		
		$consumer = new OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
		
		$access_token = $_SESSION['access_token'];
		$token = new OAuthConsumer($access_token['oauth_token'], $access_token['oauth_token_secret']);
		
		$signing_url = 'https://api.twitter.com/1/account/verify_credentials.json';
		$request = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $signing_url, array());

		$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, $token);
		$oauth_header = $request->to_xauth_header();
		
		$header = array ('X-Auth-Service-Provider: https://api.twitter.com/1/account/verify_credentials.json', 'X-Verify-Credentials-Authorization: '.$oauth_header);
		
		return $this->objectify( $this->process( $url, $postdata, $header ) );
	}
}
?>
